import QtQuick 2.4
import "Database.js" as Database

MainPageForm {
    Component.onCompleted: {
        Database.initialize();
        listModelFromDatabase(7);
    }

    buttonPage {

        onStartTask: {
            var buttonStatus = buttonPage.text1.text;
            console.log(buttonStatus)
            if(buttonStatus=="启动") {
                /*if(!swmStatus.doorStatus) {
                    buttonPage.text6.text = "洗鞋机门未关闭！";
                    buttonPage.text6.opacity = 1.0;
                    buttonPage.clipText.to = 0.0;
                    buttonPage.clipText.running = true;
                    return;
                } else if(!swmStatus.doorSwitch) {
                    buttonPage.text6.text = "洗鞋机门锁未关闭！";
                    buttonPage.text6.opacity = 1.0;
                    buttonPage.clipText.to = 0.0;
                    buttonPage.clipText.running = true;
                    return;
                } else if(!swmStatus.annexA) {
                    buttonPage.text6.text = "添加液A空！";
                    buttonPage.text6.opacity = 1.0;
                    buttonPage.clipText.to = 0.0;
                    buttonPage.clipText.running = true;
                    return;
                } else if(!swmStatus.annexB) {
                    buttonPage.text6.text = "添加液B空！";
                    buttonPage.text6.opacity = 1.0;
                    buttonPage.clipText.to = 0.0;
                    buttonPage.clipText.running = true;
                    return;

                }*/
                buttonPage.text1.text = "取消";
                //创建任务
                var taskID = Database.createTask();
                if(taskID<0) {
                    buttonPage.text6.text = "启动失败！";
                    buttonPage.text6.opacity = 1.0;
                    buttonPage.clipText.to = 0.0;
                    buttonPage.clipText.running = true;
                    return;
                }
                Database.createTaskProcessFromMode(swmStatus.currentModeID,taskID);
                Database.updateStatusForOthers();
                Database.updateStatus(taskID,1,'执行中');
                var allTime = Database.getTimeFromID(taskID);
                progressBar.indeterminate = true;
            } else if(buttonStatus=="取消") {
                buttonPage.text1.text = "启动";
                progressBar.indeterminate = false;
            } else if(buttonStatus=="完成") {
                buttonPage.text1.text = "启动";
            }
        }
    }


    modePage.onSelectModelID: {
        listModelFromDatabase(id);
        swmStatus.currentModeID = id;
    }

    function listModelFromDatabase(id) {
        taskModeModel.clear();
        var xrows = Database.getModelFromModel(id)
        if (xrows.length > 0) {
            var xindex = 0;
            while(xindex < xrows.length) {
                var xaiitem = xrows.item(xindex);
                var modelName = Database.getTaskModeName(xaiitem.Model)
                var modeIconUrl = "/images/ghost.png";
                if(modelName.indexOf("洗涤") > -1) {
                    modeIconUrl = "/images/water.png";
                } else if(modelName.indexOf("漂洗") > -1) {
                    modeIconUrl = "/images/wash.png";
                } else if(modelName.indexOf("高温消毒") > -1) {
                    modeIconUrl = "/images/sunny.png";
                } else if(modelName.indexOf("烘干") > -1) {
                    modeIconUrl = "/images/tool.png";
                }

                taskModeModel.append({"modeIcon":modeIconUrl,
                                      "modeName":modelName,
                                      "dosageTxt":xaiitem.DetergentDose,
                                      "tempTxt":xaiitem.Temperature,
                                      "timeTxt":xaiitem.Time});
                xindex++;
            }
        }
    }
}

